package com.aier.proxy.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.proxy.entity.Proxy;
import com.aier.proxy.entity.ProxyCity;


public class ProxyRowMapper implements RowMapper<Proxy> {

    private int type;
    
    public static final int PROXY_AND_PROXYCITY = 1;
    
    public ProxyRowMapper() {
    }

    public ProxyRowMapper(int type) {
        this.type = type;
    }

    @Override
    public Proxy mapRow(ResultSet rs, int rowNum) throws SQLException {
        switch (type) {
            case PROXY_AND_PROXYCITY:
                Proxy p = parseProxy(new Proxy(), rs, "p");
                p.setProxyCity(ProxyCityRowMapper.parseProxyCity(new ProxyCity(), rs, "pc"));
                return p;
        
        }
        return parseProxy(new Proxy(), rs);
    }

    public static Proxy parseProxy(Proxy p, ResultSet rs) throws SQLException {
        return parseProxy(p, rs, null);
    }

    public static Proxy parseProxy(Proxy p, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            p.setPid(rs.getInt(su+"pid"));
        } catch (SQLException e) {
            p.setPid(null);
        }

        try {
            p.setName(rs.getString(su+"name"));
        } catch (SQLException e) {
            p.setName(null);
        }
        try {
            p.setPtype(rs.getInt(su+"ptype"));
        } catch (SQLException e) {
            p.setPtype(null);
        }
        try {
            p.setAccount(rs.getString(su+"account"));
        } catch (SQLException e) {
            p.setAccount(null);
        }
        try {
            p.setEmail(rs.getString(su+"email"));
        } catch (SQLException e) {
            p.setEmail(null);
        }
        try {
            p.setPhone(rs.getString(su+"phone"));
        } catch (SQLException e) {
            p.setPhone(null);
        }
    
        
        return p;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, Proxy p) {
        par.put("pid", p.getPid());
        par.put("name", p.getName());
        par.put("ptype", p.getPtype());
        par.put("account", p.getAccount());
        par.put("email", p.getEmail());
        par.put("phone", p.getPhone());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, Proxy p) {
        args.put("pid",p.getPid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final Proxy p) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(p),
                        new String[] { "pid" });
                parsePS(ps, p);
                return ps;
            }

        };
    }

    private static String parseSQL(Proxy p) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into proxy(");
        int count = 0;
        if (null != p.getName()) {
            sql.append("name,");
            count++;
        }
        if (null != p.getPtype()) {
            sql.append("ptype,");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }
    
    private static void parsePS(PreparedStatement ps, Proxy p)
        throws SQLException {
        int count = 1;
        if (null != p.getName()) {
            ps.setString(count++, p.getName());
        }
        if (null != p.getPtype()) {
            ps.setInt(count++, p.getPtype());
        }
        if (null != p.getAccount()) {
            ps.setString(count++, p.getAccount());
        }
        if (null != p.getEmail()) {
            ps.setString(count++, p.getEmail());
        }
        if (null != p.getPhone()) {
            ps.setString(count++, p.getPhone());
        }
        
    }
    
}
